//
//  BMHAppClient.m
//  ZIPTest
//
//  Created by 爆米花 on 16/10/10.
//  Copyright © 2016年 ASASDA. All rights reserved.
//

#import "BMHAppClient.h"
#import "AFNetworking.h"

static NSString * const APIBaseURLString       = @"http://10.0.0.2:8866";
static NSString * const APIBaseURLPicture      = @"http://api.xiuquapp.com/";//http://mmapp.interface.baomihua.com/
static NSString * const APIBaseURLGame         = @"http://youxi.baomihua.com";
//static NSString * const APIBaseBMHVideoURL     = @"http://xinliao.interface.baomihua.com";
static NSString * const APIBaseBMHVideoURL     = @"http://xinliao.xiuquapp.com";
static NSString * const APITestBMHVideoURL     = @"http://test.xinliao.interface.baomihua.com";
static NSString * const APIBaseBMHVideoURLTest = @"http://app.interface.test.baomihua.com";


@implementation BMHAppClient

+ (instancetype)sharedClient
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:kBaseURL]];
    });
    BOOL isTest = [[UserDefaults objectForKey:BMHIsTest] boolValue];
    if (isTest) {
        return [BMHAppClient sharedTestClient];
    }
    return _sharedClient;
}

+(instancetype)sharedPostZHIFU
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://test.xinliao.interface.baomihua.com"]];
        
    });
    return _sharedClient;
    
}

+ (instancetype)sharedTestClient
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://testapi.xiuquapp.com"]];
        
    });
    return _sharedClient;
}

+ (instancetype)sharedClientPicture
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:kBaseURL]];
        
    });
    
    return _sharedClient;
    
}

+ (instancetype)sharedClientGame
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:APIBaseURLGame]];
        
    });
    
    return _sharedClient;

    
}

+(instancetype)sharedClientStats
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:kBMHStatBaseURL]];
        
    });
    
    return _sharedClient;

    
}

+(instancetype)sharedClientTMLogin
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:APIBaseURLString]];
        
    });
    
    return _sharedClient;
    
}

+(instancetype)sharedClientKK
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://app.interface.baomihua.com"]];
        
    });
    
    return _sharedClient;
    
}

+(instancetype)sharedTest
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://testapi.xiuquapp.com"]];
        
    });
    
    return _sharedClient;
    
}

+(instancetype)bmhVide
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:APIBaseBMHVideoURL]];
        
    });
    
    BOOL isTest = [[UserDefaults objectForKey:XLIsTest] boolValue];
    if (isTest) {
        return [BMHAppClient bmhVideoTest];
    }
    return _sharedClient;
}



+(instancetype)bmhVideoTest
{
    static BMHAppClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[BMHAppClient alloc] initWithBaseURL:[NSURL URLWithString:APITestBMHVideoURL]];
        
    });
    
    return _sharedClient;
}
#pragma mark - 重写initWithBaseURL
/**
 *
 *
 *  @param url baseUrl
 *
 *  @return 通过重写夫类的initWithBaseURL方法,返回网络请求类的实例
 */

-(instancetype)initWithBaseURL:(NSURL *)url
{
    
    if (self = [super initWithBaseURL:url]) {
        /**设置请求超时时间*/
        self.requestSerializer.timeoutInterval = 3;
        /**设置相应的缓存策略*/
        self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        /**分别设置请求以及相应的序列化器*/
        self.requestSerializer = [AFHTTPRequestSerializer serializer];
        AFJSONResponseSerializer * response = [AFJSONResponseSerializer serializer];
        response.removesKeysWithNullValues = YES;
        self.responseSerializer = response;
        
        
        /**复杂的参数类型 需要使用json传值-设置请求内容的类型*/
        [self.requestSerializer setValue:@"text/json" forHTTPHeaderField:@"Content-Type"];
        
        /**设置接受的类型*/
        [self.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/plain",@"application/json",@"text/json",@"text/javascript",@"text/html",nil]];
        
    }
    
    return self;
}

@end


